<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('实训试卷列表')" />
</head>
<style>
    .btn-info{
    margin-left:10px;
    }
    </style>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>

                            <li>
                                <label>试卷名称：</label>
                                <input type="text" name="name"/>
                            </li>
                            <li>
                                <label>考试开始时间：</label>
                                <input type="text" class="time-input" placeholder="请选择考试开始时间" name="startTime"/>
                            </li>
                            <li>
                                <label>考试结束时间：</label>
                                <input type="text" class="time-input" placeholder="请选择考试结束时间" name="endTime"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="jxsx:paper:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="jxsx:paper:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="jxsx:paper:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="jxsx:paper:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('jxsx:paper:edit')}]];
        var removeFlag = [[${@permission.hasPermi('jxsx:paper:remove')}]];
        var prefix = ctx + "jxsx/paper";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "实训试卷",
                columns: [{
                    checkbox: true
                },
               {
                    title: "序号",
                    formatter: function (value, row, index) {
                 	    return $.table.serialNumber(index);
                    }
                },


                {
                    field: 'name',
                    title: '试卷名称'
                },
                {
                    field: 'startTime',
                    title: '考试开始时间'
                },
                {
                    field: 'endTime',
                    title: '考试结束时间'
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function(value, row, index) {
                        if (value === '0') {
                            return '<span class="label label-warning">未发布</span>';
                        } else if (value === '1') {
                            return '<span class="label label-info">未开始</span>';
                        } else if (value === '2') {
                            return '<span class="label label-success">考试中</span>';
                        } else if (value === '3') {
                            return '<span class="label label-danger">已结束</span>';
                        }
                        return value;
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        // Get user role from session
                        var userRole = [[${userRole}]];
                        if (userRole === 'guan'||userRole === 'teacher') {
                            // Teacher operations
                            if(row.status === '0'){
                              actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.paperId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                              actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="smartPaper(\'' + row.paperId + '\')"><i class="fa fa-magic"></i>智能组卷</a> ');
                              actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.paperId + '\')"><i class="fa fa-remove"></i>删除</a>');
                            }else if(row.status != '1'){
                              actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="report(\'' + row.paperId + '\')"><i class="fa fa-eye"></i>查看报告</a>');
                            }
                          actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="previewPaper(\'' + row.paperId + '\')"><i class="fa fa-eye"></i>预览</a> ');
                        } else if (userRole === 'student') {
                            // Student operations
                            var currentTime = new Date();
                            var startTime = new Date(row.startTime);
                            var endTime = new Date(row.endTime);
                            
                            if (currentTime >= startTime && currentTime <= endTime) {
                                if(row.score==null||row.score==""){
                                    actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="enterExam(\'' + row.paperId + '\')"><i class="fa fa-sign-in"></i>进入考试</a> ');
                                }
                              }
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="detail(\'' + row.paperId + '\')"><i class="fa fa-eye"></i>查看</a>');
                        }
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        function smartPaper(paperId) {
            $.modal.open("智能组卷", prefix + "/smartPaper/" + paperId);
        }

        function previewPaper(paperId) {
            $.modal.open("试卷预览", prefix + "/preview/" + paperId);
        }
           function enterExam(paperId) {
            $.modal.open("答题页", prefix + "/exam/" + paperId);
        }

        function detail(paperId) {
            $.modal.open("试卷查看", prefix + "/detail/" + paperId);
        }
         function report(paperId) {
            $.modal.open("报告查看", prefix + "/report/" + paperId);
        }
    </script>
</body>
</html>